/*=============================================================================*
 *  Copyright 2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *=============================================================================*/
package topics.expression.impl;

import topics.Topic;
import topics.TopicSet;
import topics.TopicSpace;
import topics.TopicSpaceSet;
import topics.expression.InvalidTopicExpressionException;
import topics.expression.TopicExpression;
import topics.expression.TopicExpressionEvaluator;
import topics.expression.TopicExpressionException;
import topics.expression.TopicExpressionResolutionException;
import topics.expression.TopicPathDialectUnknownException;
import topics.expression.impl.AbstractTopicExpressionEvaluator;
import topics.v2004_06.TopicsConstants;
import util.xml.NamespaceContext;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Topic expression evalutor for the WS-Topics "Concrete" topic dialect.
 *
 * @see org.apache.ws.notification.topics.expression.TopicExpressionEvaluator
 *
 * @author Ian Springer (ian DOT springer AT hp DOT com)
 */
public class ConcreteTopicExpressionEvaluator
   extends AbstractTopicExpressionEvaluator
{
   //private static final Log LOG = LogFactory.getLog(ConcreteTopicExpressionEvaluator.class.getName());
   private static final String[] SUPPORTED_DIALECTS = 
                                                      {
                                                         TopicsConstants.TOPIC_EXPR_DIALECT_CONCRETE
                                                      };

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public String[] getDialects(  )
   {
      return SUPPORTED_DIALECTS;
   }

   /**
    * DOCUMENT_ME
    *
    * @param topicSpaceSet DOCUMENT_ME
    * @param topicExpr DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    *
    * @throws TopicPathDialectUnknownException DOCUMENT_ME
    * @throws TopicExpressionResolutionException DOCUMENT_ME
    * @throws InvalidTopicExpressionException DOCUMENT_ME
    * @throws TopicExpressionException DOCUMENT_ME
    */
   public Topic[] evaluate( TopicSpaceSet   topicSpaceSet,
                            TopicExpression topicExpr )
   throws TopicPathDialectUnknownException, 
          TopicExpressionResolutionException, 
          InvalidTopicExpressionException, 
          TopicExpressionException
   {
      String           expr          = getContent( topicExpr );
      NamespaceContext nsContext     = getNamespaceContext( topicExpr );
      QName            topicPath     = toQName( expr, nsContext );
      List             matchedTopics = evaluateTopicPath( topicSpaceSet, topicPath );
      return (Topic[]) matchedTopics.toArray( new Topic[0] );
   }

   private List evaluateTopicPath( TopicSpaceSet topicSpaceSet,
                                   QName         topicPath )
   throws TopicExpressionResolutionException, 
          InvalidTopicExpressionException
   {
      List       matchedTopics = new ArrayList(  );
      TopicSpace topicSpace = getTopicSpace( topicSpaceSet, topicPath );
      if ( ( topicPath.getLocalPart(  ).indexOf( "//" ) != -1 )
           || ( topicPath.getLocalPart(  ).indexOf( "*" ) != -1 )
           || ( topicPath.getLocalPart(  ).indexOf( "|" ) != -1 )
           || ( topicPath.getLocalPart(  ).indexOf( "." ) != -1 ) )
      {
         throw new InvalidTopicExpressionException( "Topic path '" + topicPath
                                                    + "' contains one or more illegal characters ('//', '*', '|' or '.')." );
      }

      StringTokenizer pathTokenizer = new StringTokenizer( topicPath.getLocalPart(  ),
                                                           "/" );
      TopicSet        topicSet     = topicSpace;
      boolean         resolvedPath = true;
      while ( pathTokenizer.hasMoreTokens(  ) )
      {
         String name = pathTokenizer.nextToken(  );
         if ( !topicSet.containsTopic( name ) )
         {
            resolvedPath = false;
            break;
         }

         topicSet = topicSet.getTopic( name );
      }

      if ( resolvedPath )
      {
         matchedTopics.add( topicSet );
      }

      return matchedTopics;
   }
}